<?php
/**
 * 会员中心
 * User: chen guang
 * Date: 2017/9/29 9:00
 *
 */

namespace app\admin\Controller;
use think\Db;
use think\Request;

class Coupon extends Common{
	
	/*
	*	构造函数
	*/
	function __construct(){
		parent::__construct();
	}

    /**
     * 优惠券列表
     */
    public function lists(){
		$where['deletes']=1;
		$where['endtime'] = array('gt',time());
		$status=input("status");
		if($status!=''){
			$where['status']=$status;
		}
        $list = db("coupon")->where($where)->order("id desc")->paginate(10);
        $this->assign('list',$list);
		$this->assign('status',input("status"));
        return $this->fetch();
    }
	/**
     * 添加优惠券
     */
    public function add(){
    	$arr[0]='星期一';
    	$arr[1]='星期二';
    	$arr[2]='星期三';
    	$arr[3]='星期四';
    	$arr[4]='星期五';
    	$arr[5]='星期六';
    	$arr[6]='星期日';
		$this->assign('arr',$arr);
        return $this->fetch();
    }
	/**
	 * 添加优惠券
	 */
	public function addmember(){
		return $this->fetch();
	}
	/*
  * 删除
  */
	public function del(){
		$id = input('id/d');
		if(!in_array(input('delete'),array(0,1))){
			$this->error("状态有误");
		}
		$data['deletes'] = input('del');
		$res=db("coupon")->where("id='$id'")->update($data);
		if($res) $this->redirect($_SERVER['HTTP_REFERER']);
		$this->error("操作失败");
	}
	/*
  * 领券中心背景图
  */
	public function background(){
		
		if(Request::instance()->isPost()){
			$data = array();
			$files = request()->file('images');
			if (!empty($files)) {
                $info = $files->move(ROOT_PATH . 'public/static/uploads');
                if($info){
					$data['images'] = $info->getSaveName();
                }  
			}
			$update = db('background')->where("id=1")->update($data);
			if($update) $this->redirect("coupon/background");
			$this->error("操作失败");
		}
		$find = db('background')->where("id=1")->find();
		$this->assign("data",$find);
		return $this->fetch();
			
	}
	/*
  * 消息提醒banner
  */
	public function tip(){
		
		if(Request::instance()->isPost()){
			$data = array();
			$files = request()->file('images');
			if (!empty($files)) {
                $info = $files->move(ROOT_PATH . 'public/static/uploads');
                if($info){
					$data['images'] = $info->getSaveName();
                }  
			}
			$update = db('background')->where("id=2")->update($data);
			if($update) $this->redirect("coupon/tip");
			$this->error("操作失败");
		}
		$find = db('background')->where("id=2")->find();
		$this->assign("data",$find);
		return $this->fetch();
			
	}
}